<?php

  class StreamWriter extends Object implements IIO {

    /** @var String The name of the file to work with. */
    private $filename;

    /** @var resource The handle of the file when it's open. */
    private $handle = null;

    /**
     * Open the file specified in parameter.
     * @param String $filename The name of the file to work with.
     */
    public function __construct(String $filename) {
      $this->filename = $filename;
      $this->handle = fopen($filename->toString(), "w+");
    }

    /**
     * Close the handle on object destruction.
     * @link http://be.php.net/manual/en/function.is-null.php
     */
    public function __destruct() {
      if ($this->handle != null) {
        $this->close();
      }
    }

    /**
     * Close the open handle of the file.
     * @return Boolean Returns true on succes or false on failure.
     * @link http://be.php.net/manual/en/function.fclose.php
     */
    public function close() {
      if ($this->handle != null) {
        return Boolean::parseBoolean(fclose($this->handle));
      }
    }

    /**
     * Writes the contents of $string to the open file.
     * @param String $string The string to be written.
     * @return Integer Returns the number of bytes written.
     * @link http://be.php.net/manual/en/function.is-int.php
     * @link http://be.php.net/manual/en/function.fwrite.php
     */
    public function write(String $string) {
      $written = fwrite($this->handle, $string->toString());

      if ($written !== false && is_int($written)) {
        return new Integer($written);
      } else {
        return new Integer(0);
      }
    }

    /**
     * Writes the contents of $string terminated by a CR LF.
     * @param String $string The string to be written.
     * @return Integer Returns the number of bytes written.
     * @link http://be.php.net/manual/en/function.is-int.php
     * @link http://be.php.net/manual/en/function.fwrite.php
     */
    public function writeLine($string) {
      $written = fwrite($this->handle, $string->toString() . self::CRLF);

      if ($written !== false && is_int($written)) {
        return new Integer($written);
      } else {
        return new Integer(0);
      }
    }

    /**
     * Write a string to a file.
     * @param String $filename The filename to write.
     * @param mixed $data This could be a string, an array or a resource.
     * @return Integer The number of bytes that were written to the file.
     * @link http://be.php.net/manual/en/function.is-int.php
     * @link http://be.php.net/manual/en/function.file-put-contents.php
     */
    public static function writeToFile(String $filename, $data) {
      if (is_string($data) || is_array($data) || is_resource($data)) {
        $written = file_put_contents($filename->toString(), $data);

        if ($written !== false && is_int($written)) {
          return new Integer($written);
        } else {
          return new Integer(0);
        }
      } else {
        return new Integer(0);
      }
    }

  }

?>
